package de.unihannover.hci.portraitquiz.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import de.unihannover.hci.portraitquiz.model.Category;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class CategoriesAdapter extends BaseAdapter {
	private Context mContext;
	private List<Category> categories;
	private AssetManager assetManager;

	public CategoriesAdapter(Context c, List<Category> categories) {
		mContext = c;
		this.categories = categories;
		this.assetManager = c.getAssets();
	}

	public int getCount() {
		return categories.size();
	}

	public Object getItem(int position) {
		return categories.get(position);
	}

	public long getItemId(int position) {
		return categories.get(position).getId();
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}
		InputStream ims = null;
		try {
			ims = assetManager.open("categories/" + categories.get(position).getImage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// create drawable from stream
		Drawable d = Drawable.createFromStream(ims, null);
		imageView.setImageDrawable(d);
		return imageView;
	}

	// references to our images
	private Integer[] mThumbIds = {

	};
}